package com.chima.utils;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;


public class TestAll {
//	public static ApplicationContext act = new ClassPathXmlApplicationContext("/applicationContext.xml");
//	public void testjdbc(){
//		ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
//		
//	}
	public static void main(String arg[]) throws ParseException{
		String nowdate = "2015-02-02";
		String rzdate = "2014-02-02";
//		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
//		Date ndate = sdf.parse(nowdate);
//		Date rdate = sdf.parse(rzdate);
//		Long s = ndate.getTime() - rdate.getTime();
//		Long a = s/(1000*3600*24*365);
//		System.out.println(ndate.getTime() );
		
		System.out.println(s(nowdate,rzdate));
//		System.out.println(act.getBean("rateDao"));
		setTimeMidNight();
		System.out.println("dkajskdjaksjdkaj:"+getDayByYearAndMonth("2015-02"));
	}
	private static int s (String a , String b) throws ParseException{
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		Date ndate = sdf.parse(a);
		Date rdate = sdf.parse(b);
		Calendar aa = Calendar.getInstance();
		Calendar bb = Calendar.getInstance();
		aa.setTime(ndate);
		bb.setTime(rdate);
		setTimeMidNight(aa);
		setTimeMidNight(bb);
		long aaMs = aa.getTimeInMillis();
		long bbMs = bb.getTimeInMillis();
		long ab = aaMs - bbMs;
		return millsecondTodays(ab);
	}
	private static int millsecondTodays(long s){
		return (int)(s/(1000*86400));
	}
	private static void setTimeMidNight(Calendar c){
		c.set(c.HOUR_OF_DAY,0);
		c.set(c.MINUTE,0);
		c.set(c.SECOND, 0);
	}

	private static int setTimeMidNight(){
	Calendar cal = Calendar.getInstance();
	cal.set(Calendar.MONTH,cal.get(Calendar.MONTH)+1);
	cal.set(Calendar.DAY_OF_MONTH,1);
	cal.set(Calendar.DATE,cal.get(Calendar.DATE)-1);
	int days=cal.get(Calendar.DAY_OF_MONTH);
	System.out.println("当月天数:"+cal.get(Calendar.DAY_OF_MONTH));
	return days;
	
	}
	

	private static int getDayByYearAndMonth(String date){
		String dyear=date.substring(0, 4);
		String dmouth=date.substring(5, 7);
	     SimpleDateFormat simpleDate = new SimpleDateFormat("yyyy/MM");
	    Calendar rightNow = Calendar.getInstance();
	    try{
	    rightNow.setTime(simpleDate.parse(dyear+"/"+dmouth));
	    }catch(ParseException e){
	    e.printStackTrace();
	    }
	    return rightNow.getActualMaximum(Calendar.DAY_OF_MONTH);
	}

}